#include "jid.h"

namespace tinkle {

Jid::Jid() {
    _node = "";
    _domain = "";
    _resource = "";
}

Jid::Jid(const std::string &jidString) {
    size_t atPos = jidString.find("@");
    _node = jidString.substr(0, atPos);
    size_t delimPos = jidString.find("/");
    if (delimPos != std::string::npos) {
        // a delimiter "/" was found
        _domain = jidString.substr(atPos+1, delimPos-atPos-1);
        _resource = jidString.substr(delimPos+1);
    } else {
        _domain = jidString.substr(atPos+1);
        _resource = "";
    }
}

Jid::Jid(const std::string &node, const std::string &domain,
    const std::string &resource) {
    _node = node;
    _domain = domain;
    _resource = resource;
}

Jid::~Jid() {
    // pass
}

bool Jid::operator == (const Jid &other) const {
    if (_node == other.Node() &&
        _domain == other.Domain()) {
        return true;
    }
    return false;
}

bool Jid::FullEquals(const Jid &other) const {
    if (_node == other.Node() &&
        _domain == other.Domain() &&
        _resource == other.Resource()) {
        return true;
    }
    return false;
}

std::string Jid::Str() const {
    std::string _str(_node+"@"+_domain);
    if (_resource != "") {
        _str += "/"+_resource;
    }
    return _str;
}

std::string Jid::Node() const {
    return _node;
}

void Jid::SetNode(const std::string &node) {
    _node = node;
}

std::string Jid::Domain() const {
    return _domain;
}

void Jid::SetDomain(const std::string &domain) {
    _domain = domain;
}

std::string Jid::Resource() const {
    return _resource;
}

void Jid::SetResource(const std::string &resource) {
    _resource = resource;
}

}